print("DBG:Initialized done.\n")

local base = _G
module("main")

local os = base.os.getenv("OS")
if os == "Windows_NT" then
	rootdir = ".\\"
else
	rootdir = "\\Program Files\\wmssystem\\"
end

function main()
	local file = base.io.open(rootdir .. "tmp\\form.txt","w")
	file:write("main")
	file:close()
	
	r = base.os.remove(rootdir .. "tmp\\exit.txt")
	if r == false then
		r = "False"
	else
		r = "True"
	end
	
	local death_count = 0
	while 1 do
		local death_lock = base.io.open(rootdir .. "tmp\\death_lock.txt","w")
		death_lock:write("1")
		death_lock:close()
		
		local exit = base.io.open(rootdir .. "tmp\\exit.txt","r")
		if exit ~= nil then
			break
		end
		
		local file = base.io.open(rootdir .. "tmp\\form.txt","r")
		if file == nil then
			break
		end
		
		local text = file:read("*all")
		file:close()
		if text == "" then
			module = "main"
		else
			module = text
		end
		base.print("DBG:Module is " .. module)
		
		local m = base.require("Library.Utils.Utils")
		
		r = m.System(rootdir .. "LuaCEGUI.exe",module)
		base.print("DBG:GUI Exited with number " .. r .. ".\n")
		
		death_lock = base.io.open(rootdir .. "tmp\\death_lock.txt","r")
		if death_lock ~= nil then
			base.print("DBG:GUI Dead.Exit.\n")
			break
		end
	end
	base.print("DBG:Program exited.\n")
end